package com.fypg.module.medical.controller.admin.hospitalarea;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.CommonResult;
import com.fypg.framework.common.util.object.BeanUtils;
import static com.fypg.framework.common.pojo.CommonResult.success;

import com.fypg.framework.excel.core.util.ExcelUtils;

import com.fypg.framework.apilog.core.annotation.ApiAccessLog;
import static com.fypg.framework.apilog.core.enums.OperateTypeEnum.*;

import com.fypg.module.medical.controller.admin.hospitalarea.vo.*;
import com.fypg.module.medical.dal.dataobject.hospitalarea.HospitalAreaDO;
import com.fypg.module.medical.service.hospitalarea.HospitalAreaService;

@Tag(name = "管理后台 - 医院区域信息")
@RestController
@RequestMapping("/medical/hospital-area")
@Validated
public class HospitalAreaController {

    @Resource
    private HospitalAreaService hospitalAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建医院区域信息")
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:create')")
    public CommonResult<String> createHospitalArea(@Valid @RequestBody HospitalAreaSaveReqVO createReqVO) {
        return success(hospitalAreaService.createHospitalArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新医院区域信息")
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:update')")
    public CommonResult<Boolean> updateHospitalArea(@Valid @RequestBody HospitalAreaSaveReqVO updateReqVO) {
        hospitalAreaService.updateHospitalArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除医院区域信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:delete')")
    public CommonResult<Boolean> deleteHospitalArea(@RequestParam("id") String id) {
        hospitalAreaService.deleteHospitalArea(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除医院区域信息")
                @PreAuthorize("@ss.hasPermission('medical:hospital-area:delete')")
    public CommonResult<Boolean> deleteHospitalAreaList(@RequestParam("ids") List<String> ids) {
        hospitalAreaService.deleteHospitalAreaListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得医院区域信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:query')")
    public CommonResult<HospitalAreaRespVO> getHospitalArea(@RequestParam("id") String id) {
        HospitalAreaDO hospitalArea = hospitalAreaService.getHospitalArea(id);
        return success(BeanUtils.toBean(hospitalArea, HospitalAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得医院区域信息分页")
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:query')")
    public CommonResult<PageResult<HospitalAreaRespVO>> getHospitalAreaPage(@Valid HospitalAreaPageReqVO pageReqVO) {
        PageResult<HospitalAreaDO> pageResult = hospitalAreaService.getHospitalAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HospitalAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出医院区域信息 Excel")
    @PreAuthorize("@ss.hasPermission('medical:hospital-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHospitalAreaExcel(@Valid HospitalAreaPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HospitalAreaDO> list = hospitalAreaService.getHospitalAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "医院区域信息.xls", "数据", HospitalAreaRespVO.class,
                        BeanUtils.toBean(list, HospitalAreaRespVO.class));
    }

}